<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\System\Config;
use Illuminate\Http\Request;

class BlogConfigService
{

    use Service;

    /**
     * 博客前端设置
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function ConfigIndexService()
    {
        $blogConfig      = file_get_contents(base_path('app/Modules/Blog/Config/blog_page.php'));
        $select          = [
            'name', 'url', 'title', 'word', 'riptive', 'record', 'info', 'company', 'qq', 'box', 'phone', 'fixed',
            'fax', 'image',
        ];
        $with            = [
            'file' => function ( $query ) {
                $query->select('id', 'url', 'file_url');
            },
        ];
        $blogConfigTitle = Config::FindModel([ 'status' => 2 ], $with, $select);
        return view('blog.config.index', compact('blogConfig', 'blogConfigTitle'));
    }

    /**
     * 博客前端设置保存
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ConfigStoreService( $request )
    {
        $status = $request->input('status');
        $error  = $this->getErrorCode();
        switch ( $status ) {
            case 1:
                request()->offsetSet('static', 2);
                request()->offsetSet('status', 1);
                return $this->getConfig()->ConfigStoreService($request);
                break;
            case 2:
                request()->offsetSet('static', 2);
                request()->offsetSet('status', 2);
                return $this->getConfig()->ConfigStoreService($request);
                break;
            case 3:
                request()->offsetSet('static', 2);
                request()->offsetSet('status', 4);
                return $this->getConfig()->ConfigStoreService($request);
                break;
            case 5:
                if ( ! $request->filled('config') ) return back()->with('error', $error::message(1001));
                file_put_contents(base_path('app/Modules/Blog/Config/blog_page.php'), $request->input('config'));
                $path = base_path('bootstrap/cache/config.php');
                @unlink($path);
                $base = base_path();
                exec("php $base/artisan config:cache");
                return back()->with('success', $error::message(210));
                break;
            default:
                return back()->with('error', $error::message(1001));
                break;
        }
    }
}
